package com.xxy.controller;

import com.xxy.entity.Order;
import com.xxy.entity.Product;
import com.xxy.entity.dto.CategoryQueryDTO;
import com.xxy.entity.dto.OrderDTO;
import com.xxy.entity.dto.ProductQueryDTO;
import com.xxy.entity.vo.ProductVO;
import com.xxy.result.PageResult;
import com.xxy.result.Result;
import com.xxy.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 新增商品
     * @param product
     * @return
     */
    @PostMapping("/save")
    public Result<String> save(@RequestBody Product product) {
        productService.saveProduct(product);
        return Result.success();
    }

    /**
     * 修改商品
     * @param product
     * @return
     */
    @PutMapping("/update")
    public Result<String> update(@RequestBody Product product) {
        productService.update(product);
        return Result.success();
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteOne(@PathVariable Integer id) {
        productService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 批量删除商品
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result<String> deleteBatch(@RequestBody List<Integer> ids) {
        productService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 查询
     * @param productQueryDTO
     * @return
     */
    @PostMapping("/query")
    public Result<PageResult> query(@RequestBody ProductQueryDTO productQueryDTO) {
        PageResult pageResult = productService.query(productQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping("/queryByFilter")
    public Result<List<ProductVO>> queryAll(@RequestBody(required = false) ProductQueryDTO productQueryDTO) {
        List<ProductVO> list = productService.queryByFilter(productQueryDTO);
        return Result.success(list);
    }
    @GetMapping("/queryById/{id}")
    public Result<ProductVO> queryById(@PathVariable Integer id) {
        ProductVO productVO = productService.queryById(id);
        return Result.success(productVO);
    }

    /**
     * 商品下单
     * @param orderDTO
     * @return
     */
    @PostMapping("/buyProduct")
    public Result<String> buyProduct(@RequestBody OrderDTO orderDTO) {
        return productService.buyProduct(orderDTO);
    }

    @PostMapping("/placeOrder/{ordersId}")
    public Result<String> placeOrder(@PathVariable Long ordersId) {
        return productService.placeOrder(ordersId);
    }

    @PostMapping("/refund/{ordersId}")
    public Result<String> refund(@PathVariable Long ordersId) {
        return productService.refund(ordersId);
    }
}
